package com.qf.service.impl;

import com.qf.mapper.TestBookMapper;
import com.qf.pojo.Book;
import com.qf.service.TestBookService;
import com.qf.utils.ResultMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TestBookServiceImpl implements TestBookService {

    @Autowired
    TestBookMapper testBookMapper;
    @Override
    public ResultMsg findlist(String bookName,String bookVersion,String bookType,int page,int limit) {
        List<Book> list = testBookMapper.findList(bookName,bookVersion,bookType,(page-1)*limit, limit);
        if (list.size()!=0)
            return ResultMsg.SUCCESS(0,list,list.size());
        return ResultMsg.FAILED(201,"查询失败");
    }

    @Override
    public ResultMsg findOne(int id) {
        Book one = testBookMapper.findOne(id);
        if (one!=null)
            return ResultMsg.SUCCESS(one);
        return ResultMsg.FAILED(201,"查询失败");
    }

    @Override
    public ResultMsg insert(Integer bookId,int courseId,int gradeId, String bookName, String bookVersion, String bookType, String bookPublish, String bookPic, String bookContent) {
           int add = testBookMapper.add(courseId,gradeId, bookName, bookVersion, bookType, bookPublish, bookPic, bookContent);
           if (add==0)
               return ResultMsg.FAILED(202,"新增失败");
           return ResultMsg.SUCCESS(0,null,0);
    }

    @Override
    public ResultMsg deleteByid(Integer id) {
        int i = testBookMapper.deleteByid(id);
        if (i==0)
            return ResultMsg.FAILED(203,"删除失败");
        return ResultMsg.SUCCESS(0,null,0);
    }

    @Override
    public ResultMsg deletelist(List<Integer> list) {
        int num=0;
        for (Integer integer : list) {
            int i = testBookMapper.deleteByid(integer);
            num +=i;
        }
        if (list.size()==num)
            return ResultMsg.SUCCESS(0,list,list.size());
        return ResultMsg.FAILED(204,"批量删除失败");
    }

    @Override
    public ResultMsg updateone(Integer bookId, Integer courseId, Integer gradeId, String bookName, String bookVersion, String bookType, String bookPublish, String bookPic, String bookContent) {
        int updateone = testBookMapper.updateone(bookId, courseId, gradeId, bookName, bookVersion, bookType, bookPublish, bookPic, bookContent);
        if (updateone==0)
            return ResultMsg.FAILED(205,"修改失败");
        return ResultMsg.SUCCESS(0,null,0);
    }
}
